package com.ato.mybatis.controller;

import com.ato.mybatis.pojo.UserPojo;
import com.ato.mybatis.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wangjing
 * @create 2021--09--23--下午 5:19
 */

@EnableAutoConfiguration
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "获取所有用户")
    @GetMapping("/all")
    public List<UserPojo> all() {
        List<UserPojo> all = userService.findAll();
        return all;
    }

    @ApiOperation(value = "测试后端程序错误")
    @GetMapping("/testError")
    public Integer testError() {
        int i = 1 / 0;
        return i;
    }

    @ApiOperation(value = "根据ID获取用户：？传参")
    @ApiImplicitParam(name = "id", value = "id", required = true)
    @GetMapping("/findUserByID")
    public UserPojo findUserByID(@RequestParam(value = "id", required = true) String id) {
        long id1 = Long.parseLong(id);
        UserPojo user = userService.findUserByID(id1);
        return user;
    }

    @ApiOperation(value = "根据ID获取用户:路径")
    @ApiImplicitParam(name = "id", value = "id", required = true)
    @GetMapping("/byPath/{id}")
    public UserPojo byPath(@PathVariable(value = "id", required = true) String id) {
        long id1 = Long.parseLong(id);
        UserPojo user = userService.findUserByID(id1);
        return user;
    }


    @PostMapping("byPost")
    public Integer byPost(@RequestBody UserPojo user) {
        UserPojo userPojo = new UserPojo();
        userPojo.setAge(user.getAge());
        userPojo.setEmail(user.getEmail());
        userPojo.setName(user.getName());

        System.out.println("usercontroller : " + user);
        userService.insertUser(userPojo);
        return 1;
    }

    @PostMapping("byPostMap")
    public String byPostMap(@RequestBody List<UserPojo> userPojos) {
        System.out.println(userPojos);
        return "user";
    }
}
